1169D - Good Triple - CodeForces Solution


brute force *1900

Please click on ads to support us..

Python Code:

s = input()
n = len(s)
v = [n] * (n+1)
ans = 0
for i in range(n-1, -1,-1):
    v[i] = v[i + 1]
    k = 1
    while i + 2 * k < v[i]:
        if (s[i] == s[i + k] and s[i + k] == s[i + 2 * k]):
            v[i] = i + 2 * k
        k += 1
    ans += n - v[i]
print(ans)

C++ Code:

/*
Problem: 1169D
Date: 22-02-2024 04:57 AM
*/


#include <bits/stdc++.h>

#define FOR(i, n) for(int i = 0; i < n; i++)
#define FORK(i, k, n) for(int i = k; i < n; i++)
#define FORr(i, n) for(int i = n - 1; i >= 0; i--)
#define FORKr(i, k, n) for(int i = n - 1; i >= k; i--)
#define PRINT(a, b) copy((a), (b), ostream_iterator<int>(cout, " "))
#define all(a) a.begin(), a.end()
#define in(a, b) ((b).find(a) != (b).end())
#define sz(a) ((int) (a).size())
#define Mp make_pair
#define PII pair<int, int>
#define ll long long
#define VI vector<int>

using namespace std;

string s;
int n;

int main() {
    std::ios_base::sync_with_stdio(false);
    cin >> s;
    n = s.length();
    int l = 0;
    ll sum = 0;
    for(int i = 0; i < n; i++) {
        for(int j = 2; j < 10 && i + j < n; j++) {
            for(int k = 1; j - 2 * k >= 0 && k < 10; k++) {
                if(s[i + j] == s[i + j - k] && s[i + j] == s[i + j - 2 * k]) {
                    sum += (i - l + 1) * (n - (i + j));
                    l = i + 1;
                    break;
                }
            }
        }
    }
    cout << sum << endl;
}


Comments

Submit
0 Comments
More Questions

1237A - Balanced Rating Changes
1616A - Integer Diversity
1627B - Not Sitting
1663C - Pōja Verdon
1497A - Meximization
1633B - Minority
688B - Lovely Palindromes
66B - Petya and Countryside
1557B - Moamen and k-subarrays
540A - Combination Lock
1553C - Penalty
1474E - What Is It
1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale